﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Photo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.Title = "我的照片上传";
        /*
        string UID = Request.QueryString["ygbh"];
        Session["PhotoFileName"] = "";
        if (UID != "" && UID != null)
        {
            string YGBH = Request.QueryString["ygbh"];
            Session["PhotoFileName"] = YGBH;
        }
        else if (Session["UID"] != null)
        {
            string YGBH = Session["ygbh"].ToString();
            Session["PhotoFileName"] = YGBH;
            if (Session["Photo"].ToString() != "" && Session["Photo"].ToString() != null)
            {
                NoteInfo.Text = "<a href=\"Default.aspx\">返回</a>";
            }
        }
        else
        {
            Session.Abandon();
            //清空
            Session.Clear();
            'Response.Redirect("Login.aspx");
        }
         * */
        //NoteInfo.Text = Session["PhotoFileName"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        /**
        Request.Files[i].ContentLength 获取上传文件的大小，以字节为单位。
        Request.Files[i].ContentType 获取客户端发送的文件的 MIME 内容类型。
        Request.Files[i].FileName
        Request.Files[i].InputStream
        Request.Files[i].SaveAs(string filename)
         **/
        HttpPostedFile f = Request.Files[0];
        string path = Server.MapPath("~/UpLoadFile/UserPhotoPath/");
        string fileExtension = System.IO.Path.GetExtension(f.FileName).ToLower();
        //判断是否已经选取文件
        if (f.FileName!="")
        {
            //对文件大小限制
             if (f.ContentLength <= 1024000) //1024*1024*10=10M,控制大小500kb
            {
                //限定只能上传jpg和gif图片
                string[] allowExtension = { ".jpg", ".gif", ".png",".aspx" };
                //对上传的文件的类型进行一个个匹对
                int j = 0;
                for (int i = 0; i < allowExtension.Length; i++)
                {
                    if (fileExtension == allowExtension[i])
                    {
                        //string YGBH = Session["PhotoFileName"].ToString();
                        string YGBH = "11";

                        f.SaveAs(path + YGBH + fileExtension);
                        scjrm.WebServiceGZ Web = new scjrm.WebServiceGZ();
                        string info = Web.UpUserPhoto("shensoft", YGBH, "/UpLoadFile/UserPhotoPath/" + YGBH + fileExtension);
                        if (info == "true")
                        {
                            //NoteInfo.Text = "<br><a href=\"Login.aspx?q=quit\">拍照上传成功请重新登录</a>";
                            Session["Photo"] = "/UpLoadFile/UserPhotoPath/" + YGBH + fileExtension;
                            NoteInfo.Text = "<a href=\"Default.aspx\">拍照上传成功请点击返回</a>";
                        }
                        else
                        {
                            NoteInfo.Text = "<br><a href=\"Login.aspx?q=quit\">上传失败请重新拍摄</a>";
                        }
                        return;
                    }
                    else
                    {
                        j++;
                    }
                }
                if (j > 0)
                {
                    Response.Write("<script>alert('文件格式不正确');</script>");
                    return;
                }

            }
            else {

                Response.Write("<script>alert('文件大小不正确，请确保文件不大于1MB');</script>");
                return;
            }




            }
            else
            {
                Response.Write("<script>alert('你还没有选择文件');</script>");
                return;
            }
    }

}
